//
//  UILabel+Extension.swift
//  AispeechMobileApp
//
//  Created by Lee on 2019/2/27.
//  Copyright © 2019年 Lee. All rights reserved.
//

import UIKit

extension UILabel {
    
    //MARK:部分字体变颜色
    /// - Parameters:
    ///   - rangeText:需要改变颜色的字符串
    ///   - rangeColor: 颜色值
    func stringChangeTextColor(rangeText:String, rangeColor:UIColor) {
        
        let attrstring:NSMutableAttributedString = NSMutableAttributedString(string: self.text ?? "")
        let str = NSString(string: self.text ?? "")
        let theRange = str.range(of: rangeText)
        attrstring.addAttribute(NSAttributedString.Key.foregroundColor, value: rangeColor, range: theRange)
        self.attributedText = attrstring
    }
    
    //MARK: - label根据特定字符来区分显示两种颜色、大小的字体（特定字符串包含在前面一段）
    /// label根据特定字符来区分显示两种颜色、大小的字体
    ///
    /// - Parameters:
    ///   - rangeText: 分隔字符串（字符串包含在前面一段）
    ///   - firColor: 第一种颜色
    ///   - secColor: 第二种颜色
    ///   - firFontSize: 第一种字体大小
    ///   - secFontSize: 第二种字体大小
    func twoDifferentTextColor(rangeText:String, firColor:UIColor, secColor:UIColor, firFontSize:CGFloat, secFontSize:CGFloat) {
        let string = self.text
        let ranStr = rangeText
        let attrstring:NSMutableAttributedString = NSMutableAttributedString(string:string ?? "")
        let str = NSString(string: string ?? "")
        let theRange = str.range(of: ranStr)
        attrstring.addAttribute(NSAttributedString.Key.foregroundColor, value: firColor, range: NSMakeRange(0, theRange.location + 1))
        attrstring.addAttribute(NSAttributedString.Key.foregroundColor, value: secColor, range: NSMakeRange(theRange.location + 1, (string?.count ?? 0) - (theRange.location + 1)))
        attrstring.addAttribute(NSAttributedString.Key.font, value: UIFont.systemFont(ofSize: firFontSize), range: NSMakeRange(0, theRange.location + 1))
        attrstring.addAttribute(NSAttributedString.Key.font, value: UIFont.systemFont(ofSize: secFontSize), range: NSMakeRange(theRange.location + 1, (string?.count ?? 0) - (theRange.location + 1)))
        
        self.attributedText = attrstring
    }
    
    //MARK: - 改变行间距
    func changeLineSpaceForLabel(space:CGFloat) {
        let labelText = self.text
        let attributedString = NSMutableAttributedString.init(string: labelText ?? "")
        let paragraphStyle = NSMutableParagraphStyle.init()
        paragraphStyle.lineSpacing = space
        attributedString.addAttribute(NSAttributedString.Key.paragraphStyle, value: paragraphStyle, range: NSRange(location: 0, length: labelText?.count ?? 0))
        self.attributedText = attributedString
    }
    
    //MARK: - 改变字间距
    func changeWordSpaceForLabel(space:CGFloat) {
        let labelText = self.text
        let attributedString = NSMutableAttributedString.init(string: labelText ?? "", attributes: [NSAttributedString.Key.kern : space])
        let paragraphStyle = NSMutableParagraphStyle.init()
        attributedString.addAttribute(NSAttributedString.Key.paragraphStyle, value: paragraphStyle, range: NSRange(location: 0, length: labelText?.count ?? 0))
        self.attributedText = attributedString
    }
    
    //MARK: - 改变行间距与字间距
    func changeSpaceForLabel(lineSpace:CGFloat,wordSpace:CGFloat) {
        let labelText = self.text
        let attributedString = NSMutableAttributedString.init(string: labelText ?? "", attributes: [NSAttributedString.Key.kern : wordSpace])
        let paragraphStyle = NSMutableParagraphStyle.init()
        paragraphStyle.lineSpacing = lineSpace
        attributedString.addAttribute(NSAttributedString.Key.paragraphStyle, value: paragraphStyle, range: NSRange(location: 0, length: labelText?.count ?? 0))
        self.attributedText = attributedString
    }
}
